import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: {},

    menus: [],
    menusLoaded: false,
  },

  getters: {
    userName: (state) => {
      return state.userInfo.name || "";
    },
  },
  mutations: {
    setUser(state, payload) {
      state.userInfo = payload;
    },

    setMenus(state, payload) {
      state.menus = payload;
      state.menusLoaded = true;
    },
  },
  actions: {
    login(context) {
      Vue.http.post("login", { username: "admin", password: "admin" });

      Vue.http.get("index/info").then((result) => {
        context.commit("setUser", result.data.data.user);
      });
    },

    loadMenus({ commit }) {
      Vue.http.post("index/getMenuList").then(() => {
        const menus = [
          {
            name: "首页",
            link: "/home",
          },
          {
            name: "其他",
            children: [
              {
                name: "关于",
                children: [
                  {
                    name: "xxx",

                    link: "/about",
                  },
                  {
                    name: "xxx2",

                    link: "/about",
                  },
                ],
              },
            ],
          },
          {
            name: "设备管理",
            link: "/manage",
          },
          // {
          //   name: "安防监控",
          //   link: "monitoring",
          // },
          // {
          //   name: "警告管理",
          //   link: "warning",
          // },
          // {
          //   name: "知识库管理",
          //   link: "kbm",
          // },
        ];

        commit("setMenus", menus);
      });
    },
  },
  modules: {},
});
